package controller.priv;

import controller.MyCoolServlet;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.*;
import javax.servlet.http.*;
import model.Product;
import persistence.product.ProductDAO;
import persistence.product.ProductPersistFactory;

public class DeleteServlet extends MyCoolServlet {

    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ProductDAO persistenceManager = ProductPersistFactory.getProductDAO(persistenceMechanism);
        String name = request.getParameter("nombre");
        String so = request.getParameter("so");
        Map<String,Product> productArray = new HashMap<String, Product>();
        persistenceManager.readProduct(name+"_"+so, productArray);
        if (productArray.size()>0 && persistenceManager.deleteProduct(name+"_"+so)) {
            request.setAttribute("product", productArray.get(name+"_"+so));
            request.setAttribute("message", "Se ha eliminado " + name + " de la tienda");
            gotoURL(successForm, request, response);
        } else {
            request.setAttribute("message1", "Error al eliminar app:");
            request.setAttribute("message2", "Es posible que no exista dicha app");
            gotoURL(errorForm, request, response);
        }
    }
}
